#ifndef TCPMGR_H
#define TCPMGR_H
#include<QTcpSocket>
#include<QByteArray>
#include "global.h"
#include "Singleton.h"
#include"userdata.h"
class TcpMgr:public QObject, public Singleton<TcpMgr>,
               public std::enable_shared_from_this<TcpMgr>
{
    Q_OBJECT

public:
    TcpMgr() ;
private:
    friend class Singleton<TcpMgr>;

    void initHandlers();

    QTcpSocket _socket;
    QString _host;
    uint16_t _port;
    QByteArray _buffer;
    bool _b_recv_pedding;
    quint16 _message_id;
    quint16 _message_len;
    QMap<ReqId,std::function<void(ReqId,int len,QByteArray data)>>_handlers;
    void handleMsg(ReqId id,int len,QByteArray data);
public slots:
    void slot_tcp_connect(ServerInfo);
    void slot_send_data(ReqId reqId,QByteArray data);

signals:
    void sig_con_success(bool bsuccess);
    void sig_send_data(ReqId reqId, QByteArray data);
    void sig_login_failed(int err);
    void sig_swich_chatdlg();
    void sig_user_search(std::shared_ptr<SearchInfo>);
    void sig_auth_rsp(std::shared_ptr<AuthRsp>);
    void sig_friend_apply(std::shared_ptr<AddFriendApply>);
    void sig_add_auth_friend(std::shared_ptr<AuthInfo>);
    void sig_text_chat_msg(std::shared_ptr<TextChatMsg>);
};

#endif // TCPMGR_H
